const Base = require('../../base.js');

module.exports = class extends Base {
  async indexAction() {
    let page = parseInt(this.get('page'))
    let num = parseInt(this.get('num'))
    let type = parseInt(this.get('type'))
    num = num || 15
    page = page || 1
    type = parseInt(type) || 0
    let json = {}
    if (type === 1) {
      json.status = 1
    } else if (type === 2) {
      json.status = {
        $in: [2, -1]
      }
    }
    /// 获取任务回复
    const task_result_db = this.mongoose('mongo/task_result')
    let task_result_list = await task_result_db.find(json).sort({_id: -1}).skip((page-1) * num).limit(num) // 获取列表
    let count = await task_result_db.find(json).count() // 获取列表
    /// 获取所有关联的用户ID 和任务ID
    let uidList = [], tidList = []
    task_result_list.forEach(item => {
      uidList.push(think.objId(item.uid))
      tidList.push(think.objId(item.tid))
    })
    /// 获取关联用户
    const users_db = this.mongoose('mongo/users')
    let users_list = await users_db.find({
      _id: { $in: uidList }
    })
    /// 获取关联任务
    const task_db = this.mongoose('mongo/task')
    let task_list = await task_db.find({
      _id: { $in: tidList }
    })
    /// 返回结果
    this.json({
      status: 200,
      message: 'success',
      data: {
        result: task_result_list,
        users_list,
        task_list,
        page: {
          page, num, count
        }
      }
    })
  }
};
